﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Timers;
using System.Windows.Threading;

namespace MichaelTestApp
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{

        //private DispatcherTimer tmr;

		public Window1()
		{
            //tmr = new DispatcherTimer();
            //tmr.Tick += new EventHandler(tmr_Tick);
            //tmr.Interval = TimeSpan.FromMilliseconds(100);
			InitializeComponent();
		}

        //void tmr_Tick(object sender, EventArgs e)
        //{
        //    tmr.IsEnabled = false;
        //    Redraw(Layout());
        //}


        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            Redraw(Layout());
            base.OnRenderSizeChanged(sizeInfo);
        }

        //protected override void OnResizeEnd(EventArgs e)
        //{
        //    Redraw(Layout());
        //    base.OnResizeEnd(e);
        //}



		protected override void OnInitialized(EventArgs e)
		{
			m_textBlocks = new List<TextBlock>();
			m_graph = new List<KineticNode>()
            {
                new KineticNode(),
                new KineticNode(),
                new KineticNode(),
                new KineticNode(),
                new KineticNode(),
                new KineticNode(),
                new KineticNode(),
                new KineticNode(),
                new KineticNode(),
                new KineticNode()

            };
			Random rand = new Random();

			m_graph[0].Location = new Vector(0.5, 0.5);
			m_graph[1].Location = new Vector(800 / 3, 250);
			m_graph[2].Location = new Vector(200, 500 / 3);
			for (int iter = 0; iter < 10; iter++)
			{
				m_graph[iter].Location = new Vector(rand.NextDouble() * 300, rand.NextDouble() * 400);
			}


			int label = 0;


			foreach (KineticNode node in m_graph)
			{
				//var otherNodes = m_graph.Where(n => n != node);
				//foreach (KineticNode otherNode in otherNodes)
				//{
				//    node.AddAdjacentNode(otherNode);
				//}
				if (node != m_graph[2])
				{
					m_graph[2].AddAdjacentNode(node);
					node.AddAdjacentNode(m_graph[2]);
				}

				TextBlock tb = new TextBlock();
				tb.Text = label.ToString();
				tb.Background = Brushes.White;

				Canvas.SetLeft(tb, node.Location.X - tb.ActualWidth / 2);
				Canvas.SetTop(tb, node.Location.Y - tb.ActualHeight / 2);

				m_textBlocks.Add(tb);
				MainCanvas.Children.Add(tb);

				label++;
			}


			m_graph[0].AddAdjacentNode(m_graph[1]);
			m_graph[1].AddAdjacentNode(m_graph[0]);
			m_graph[1].AddAdjacentNode(m_graph[5]);
			m_graph[5].AddAdjacentNode(m_graph[1]);
			// m_graph[2].RemoveAdjacentNode(m_graph[0]);

			base.OnInitialized(e);
		}

		private List<KineticNode> m_graph;
		//private TextBlock m_rootTextBlock;
		private List<TextBlock> m_textBlocks;


		private void Button_Click(object sender, RoutedEventArgs e)
		{
			Random rand = new Random();
			double half = MainCanvas.ActualWidth;
			double topHalf = MainCanvas.ActualHeight / 2;
			int index = 0;
			int randEdgeA = rand.Next(10);
			int randEdgeB = rand.Next(10);
			while (randEdgeB == randEdgeA)
			{
				randEdgeB = rand.Next(10);
			}
			foreach (KineticNode node in m_graph)
			{
				node.KeyFramesX.Clear();
				node.KeyFramesY.Clear();
                //node.Location = new Vector(rand.NextDouble() * half, rand.NextDouble() * topHalf);
                //if (index > 5)
                //{
                //    node.Location = new Vector(node.Location.X, MainCanvas.ActualHeight - node.Location.Y);
                //}
				index++;
				m_graph[randEdgeA].AddAdjacentNode(m_graph[randEdgeB]);
				m_graph[randEdgeB].AddAdjacentNode(m_graph[randEdgeA]);
			}


            Redraw(Layout());
		}

        private IEnumerable<KineticNode> Layout()
        {
            IEnumerable<KineticNode> newNodes = Utility.CalculateNodePositions(m_graph, m_graph[2], MainCanvas.ActualWidth, MainCanvas.ActualHeight);
            return newNodes;
        }

        private void Redraw(IEnumerable<KineticNode> graph)
        {
            int index = 0;
       
            foreach (KineticNode node in graph)
            {

                DoubleAnimationUsingKeyFrames anim1 = new DoubleAnimationUsingKeyFrames();
                //anim1.FillBehavior =
                anim1.KeyFrames = node.KeyFramesX;

                anim1.Duration = new Duration(TimeSpan.FromSeconds(1));
                anim1.AccelerationRatio = 0.999;
                anim1.DecelerationRatio = 0.001;
                anim1.IsCumulative = true;

                DoubleAnimationUsingKeyFrames anim2 = new DoubleAnimationUsingKeyFrames();
                //anim2.FillBehavior = FillBehavior.Stop;
                anim2.KeyFrames = node.KeyFramesY;

                anim2.Duration = new Duration(TimeSpan.FromSeconds(1));
                anim2.AccelerationRatio = 0.999;
                anim2.DecelerationRatio = 0.001;

                m_textBlocks[index].BeginAnimation(Canvas.LeftProperty, anim1);
                m_textBlocks[index].BeginAnimation(Canvas.TopProperty, anim2);

                index++;
            }
        }

	}
}
